package com.rtx.config.exception;

import java.util.Locale;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import com.rtx.model.emums.EnumDataType;

/**
 * 国际化资源定义 文案转换工具类
 *
 * @author Zy
 */
@Component
public class LanguageConfig {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 获取国际化异常信息
     *
     * @param code
     * @param params
     * @return
     */
    public String getLocaleMessage(String code, Object... params) {

        String header = httpServletRequest.getHeader(EnumDataType.LANGUAGE.getValue());
        Locale locale = Objects.nonNull(header) ? new Locale(header) : Locale.getDefault();
        try {
            String message = messageSource.getMessage(code, params, locale);
            log.debug("国际化code = {}，参数集 = {}，header语言 = {}，执行语言 = {}，缺省文案 = {}", code, params, header, locale, message);
            return message;
        } catch (Exception e) {
            log.error("本地化异常消息Response类发生异常: {}, {}", code, params);
            return code;
        }
    }
}
